package com.atguigu.mq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-rabbitmq.xml")
public class Producer {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Test
    public void t1(){
        rabbitTemplate.convertAndSend("spring_direct_exchange","queue1","hello direct");
    }
    @Test
    public void t2() throws InterruptedException {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {

                if (b){
                    System.out.println("成功:"+s);
                }else {
                    System.out.println("失败:"+s);
                }
            }
        });
        rabbitTemplate.convertAndSend("spring_direct_exchange","queue2","hello direct");
//        Thread.sleep(2000);
    }

    @Test
    public void t3() throws InterruptedException {
        rabbitTemplate.setMandatory(true);//当前方法支持处理退回的模式
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println(new String(message.getBody()));
                System.out.println("状态码："+replyCode);
                System.out.println("退回原因："+replyText);
                System.out.println(exchange);
                System.out.println(routingKey);
            }
        });

        rabbitTemplate.convertAndSend("spring_direct_exchange","queue2","hello directaaaa");
//        Thread.sleep(2000);
    }


    @Test
    public void t4(){

        rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.a","hello ttl");


    }

    @Test
    public void t5(){
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.a","hello ttl",messagePostProcessor);

    }

    @Test
    public void t6(){
        //1. 测试过期时间，死信消息
        rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗？");
    }

    @Test
    public void t7(){
        //1. 测试过期时间,死信消息
        //rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗？");

        //2. 测试长度限制后,消息死信
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗？"+i);
        }
    }

    @Test
    public  void testDelay() throws InterruptedException {
        //1.发送订单消息。 将来是在订单系统中,下单成功后,发送消息
        rabbitTemplate.convertAndSend("order_exchange",
                "order.msg","订单信息：id=1,time=2020年10月17日11：41：47");

        //2.打印倒计时10秒
        for (int i = 10; i > 0 ; i--) {
            System.out.println(i+"...");
            Thread.sleep(1000);
        }
    }


}
